/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.account;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.account.SysRole;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-12-06
 */
@Service("sysroleService")
public class SysRoleServiceImpl implements SysRoleService {
    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of SysRole
     ****************************************/
    public void insert(SysRole sysrole) throws Exception {
        sysrole.setRoleId(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(sysrole);
    }

    /****************************************
     * Update a instace of SysRole
     ****************************************/
    public void update(SysRole sysrole) throws Exception {
        iDataSet4iBatis.update(sysrole);
    }

    /****************************************
     * Delete a instace of SysRole
     ****************************************/
    public void delete(SysRole sysrole) throws Exception {
        iDataSet4iBatis.delete(sysrole);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(SysRole sysrole) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(SysRole sysrole) throws Exception {
        String statementId = sysrole.getStatmentId();
        String mainId = sysrole.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> sysroleList = new ArrayList<BaseModel>();
        for (String id : ids) {
            SysRole model = new SysRole();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            sysroleList.add(model);
        }
        iDataSet4iBatis.batchDelete(sysroleList);
    }

    /****************************************
     * Query instaces of SysRole
     ****************************************/
    public List<BaseModel> queryAsList(SysRole sysrole) throws Exception {
        String queryListStatement = "";
        if (sysrole.isPaging()) {
            queryListStatement = sysrole.getStatmentId();
            sysrole.setStatmentId("SysRole.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(sysrole);
            sysrole.setTotalCount(baseModel.getTotalCount());
            sysrole.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(sysrole);
    }

    /****************************************
     * Query a instaces of SysRole
     ****************************************/
    public BaseModel queryAsObject(SysRole sysrole) throws Exception {
        return iDataSet4iBatis.queryAsObject(sysrole);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
